package com.example.fragmentTest;

import android.app.Activity;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ArrayListFragment extends ListFragment {
	
	OnItemSelectedListener listener;
	
	public static ArrayListFragment newInstance(String[] num){
		
		ArrayListFragment fragment = new ArrayListFragment();
		
		Bundle bundle = new Bundle();
		bundle.putStringArray("numberList", num);
		fragment.setArguments(bundle);
		
		return fragment;
	}
	
	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		
		try{
			listener = (OnItemSelectedListener) activity;
		}catch(Exception e){
			throw new ClassCastException(activity.toString() + 
					"interface OnItemSelectedListener");
		}
	}

	public String[] getNumbers(){
		return getArguments().getStringArray("numberList");
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);

		setListAdapter(new ArrayAdapter<String>(getActivity(),
				android.R.layout.simple_list_item_activated_1, getNumbers()));

		getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		getListView().setItemChecked(position, true);
		
		listener.onItemSelected(l, v, position, id);
	}
}
